import { contextBridge, ipcRenderer } from 'electron'

/**
 * 定义暴露给渲染进程的API类型
 */
export interface ElectronAPI {
  // 照片相关API
  photos: {
    getList: (params: {
      page?: number
      limit?: number
      orderBy?: string
      orderDirection?: 'ASC' | 'DESC'
      filters?: any
    }) => Promise<any>
    getById: (params: { id: number }) => Promise<any>
    update: (params: { id: number; updates: any }) => Promise<any>
    delete: (params: { id: number }) => Promise<any>
    deleteMultiple: (params: { ids: number[] }) => Promise<any>
    updateMultiple: (params: { ids: number[]; updates: any }) => Promise<any>
    getThumbnail: (params: { id: number }) => Promise<any>
  }
  
  // 相册相关API
  albums: {
    getList: (params: {
      page?: number
      limit?: number
      orderBy?: string
      orderDirection?: 'ASC' | 'DESC'
    }) => Promise<any>
    getById: (params: { id: number }) => Promise<any>
    create: (params: { name: string; description?: string }) => Promise<any>
    update: (params: { id: number; updates: any }) => Promise<any>
    delete: (params: { id: number }) => Promise<any>
    addPhoto: (params: { albumId: number; photoId: number }) => Promise<any>
    removePhoto: (params: { albumId: number; photoId: number }) => Promise<any>
  }
  
  // 搜索相关API
  search: {
    query: (params: {
      query?: string
      filters?: any
      page?: number
      limit?: number
    }) => Promise<any>
    photos: (params: {
      query?: string
      page?: number
      pageSize?: number
      filters?: any
    }) => Promise<any>
    albums: (params: {
      query?: string
      page?: number
      pageSize?: number
    }) => Promise<any>
    tags: (params: {
      query?: string
      page?: number
      pageSize?: number
    }) => Promise<any>
    getSuggestions: (params: { type: string; query: string }) => Promise<any>
    getPopularTags: (params?: { limit?: number }) => Promise<any>
    getAllTags: () => Promise<any>
    getCameraMakes: () => Promise<any>
    getCameraModels: (params?: { make?: string }) => Promise<any>
  }
  
  // 系统相关API
  system: {
    openFile: (params: { filePath: string }) => Promise<any>
    showInFolder: (params: { filePath: string }) => Promise<any>
    getAppVersion: () => Promise<any>
    checkForUpdates: () => Promise<any>
    selectFolder: () => Promise<any>
    selectFile: (params?: { filters?: any[]; multiple?: boolean }) => Promise<any>
    getUserDataPath: () => Promise<any>
    getTempPath: () => Promise<any>
    getDocumentsPath: () => Promise<any>
    getPicturesPath: () => Promise<any>
    showMessageBox: (params: {
      type?: 'none' | 'info' | 'error' | 'question' | 'warning'
      title: string
      message: string
      buttons?: string[]
    }) => Promise<any>
  }
  
  // 事件监听
  on: (channel: string, callback: (...args: any[]) => void) => void
  off: (channel: string, callback: (...args: any[]) => void) => void
}

/**
 * 实现暴露给渲染进程的API
 */
const electronAPI: ElectronAPI = {
  // 照片相关API
  photos: {
    getList: (params) => ipcRenderer.invoke('photos:getList', params),
    getById: (params) => ipcRenderer.invoke('photos:getById', params),
    update: (params) => ipcRenderer.invoke('photos:update', params),
    delete: (params) => ipcRenderer.invoke('photos:delete', params),
    deleteMultiple: (params) => ipcRenderer.invoke('photos:deleteMultiple', params),
    updateMultiple: (params) => ipcRenderer.invoke('photos:updateMultiple', params),
    getThumbnail: (params) => ipcRenderer.invoke('photos:getThumbnail', params)
  },
  
  // 相册相关API
  albums: {
    getList: (params) => ipcRenderer.invoke('albums:getList', params),
    getById: (params) => ipcRenderer.invoke('albums:getById', params),
    create: (params) => ipcRenderer.invoke('albums:create', params),
    update: (params) => ipcRenderer.invoke('albums:update', params),
    delete: (params) => ipcRenderer.invoke('albums:delete', params),
    addPhoto: (params) => ipcRenderer.invoke('albums:addPhoto', params),
    removePhoto: (params) => ipcRenderer.invoke('albums:removePhoto', params)
  },
  
  // 搜索相关API
  search: {
    query: (params) => ipcRenderer.invoke('search:query', params),
    photos: (params) => ipcRenderer.invoke('search:photos', params),
    albums: (params) => ipcRenderer.invoke('search:albums', params),
    tags: (params) => ipcRenderer.invoke('search:tags', params),
    getSuggestions: (params) => ipcRenderer.invoke('search:getSuggestions', params),
    getPopularTags: (params) => ipcRenderer.invoke('search:getPopularTags', params),
    getAllTags: () => ipcRenderer.invoke('search:getAllTags'),
    getCameraMakes: () => ipcRenderer.invoke('search:getCameraMakes'),
    getCameraModels: (params) => ipcRenderer.invoke('search:getCameraModels', params)
  },
  
  // 系统相关API
  system: {
    openFile: (params) => ipcRenderer.invoke('system:openFile', params),
    showInFolder: (params) => ipcRenderer.invoke('system:showInFolder', params),
    getAppVersion: () => ipcRenderer.invoke('system:getAppVersion'),
    checkForUpdates: () => ipcRenderer.invoke('system:checkForUpdates'),
    selectFolder: () => ipcRenderer.invoke('system:selectFolder'),
    selectFile: (params) => ipcRenderer.invoke('system:selectFile', params),
    getUserDataPath: () => ipcRenderer.invoke('system:getUserDataPath'),
    getTempPath: () => ipcRenderer.invoke('system:getTempPath'),
    getDocumentsPath: () => ipcRenderer.invoke('system:getDocumentsPath'),
    getPicturesPath: () => ipcRenderer.invoke('system:getPicturesPath'),
    showMessageBox: (params) => ipcRenderer.invoke('system:showMessageBox', params)
  },
  
  // 事件监听
  on: (channel, callback) => {
    // 只允许监听特定频道以提高安全性
    const validChannels = [
      'photos:import-progress',
      'photos:import-complete',
      'app:update-available'
    ]
    
    if (validChannels.includes(channel)) {
      ipcRenderer.on(channel, callback)
    }
  },
  
  off: (channel, callback) => {
    ipcRenderer.removeListener(channel, callback)
  }
}

/**
 * 将API暴露给渲染进程
 */
contextBridge.exposeInMainWorld('electronAPI', electronAPI)

// 类型声明，供渲染进程使用
declare global {
  interface Window {
    electronAPI: ElectronAPI
  }
}